package cn.sofwin.blog.servlet;

import cn.sofwin.blog.entity.User;
import cn.sofwin.blog.service.UserService;
import cn.sofwin.blog.service.impl.UserServiceImpl;
import cn.sofwin.core.utils.HttpUtils;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/blog/user/check")
public class UserCheckServlet extends HttpServlet {

    /**
     * 用户服务接口
     */
    private UserService userService = UserServiceImpl.getInstance();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 定义返回结果对象
        Map<String, Object> result = null;
        // 获取操作类型
        String action = req.getParameter("action");
        switch (action) {
            case "checkUsername":
                result = checkUsername(req);
                break;
            case "checkPassword":
                result = checkPassword(req);
                break;
            default:
                result = new HashMap<>();
                result.put("code", 0);
        }
        // 返回请求结果（JSON格式的文本）
        HttpUtils.writeJson(resp, result);
    }

    /**
     * 校验用户名是否有效
     *
     * @param request
     * @return
     */
    private Map<String, Object> checkUsername(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        String username = request.getParameter("username");

        if (StringUtils.isBlank(username)) {
            result.put("code", -1);
            result.put("message", "非法参数");
        } else {
            User user = userService.findByUsername(username);
            if (user != null) {
                result.put("code", -1);
                result.put("message", "用户名已存在");
            } else {
                result.put("code", 0);
            }
        }
        return result;
    }

    /**
     * 校验密码是否为空
     *
     * @param request
     * @return
     */
    private Map<String, Object> checkPassword(HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        String password = request.getParameter("password");
        System.out.println(password);

        if (StringUtils.isBlank(password)) {
            result.put("code", -1);
            result.put("message", "密码不能为空！");
        } else {
            result.put("code", 0);
        }
        return result;
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPut(req, resp);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doDelete(req, resp);
    }

    @Override
    protected void doOptions(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doOptions(req, resp);
    }
}